﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.Recycling.Object;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.recycling.Object.Entity;
using Imaginecup.Recycling.GameMan;
using Imaginecup.shareData;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using Imaginecup.util;
using Imaginecup.gameState.recycling.Sprite;
using Imaginecup.gameState.recycling.Sprite.Factory;
using Imaginecup.gameState.recycling;
using Imaginecup.gameState.recycling.Sprite.Behavior;
using Imaginecup.recycling.Object.Menu;

namespace Imaginecup.recycling.Object
{
    public class SlidingButton : GameObject
    {
        private IPlayer gameObject;
        private ISprite m_Button;

        public SlidingButton(IPlayer obj)
        {
            gameObject = obj;
            Updater.GetInstance().AddUpdate(this);
        }
        protected override void Initialize()
        {
            base.Initialize();
            m_TextureName = "SlidingButton";
            Texture2D m_Texture = ResourceManager.GetInstance().GetContent(m_TextureName);
            m_Info = new ObjectInfo
            (
                new Vector2(675, 379),
                new Rectangle(675, 379, m_Texture.Width/2, m_Texture.Height)
            );
            m_Info.FrameWidth = m_Texture.Width / 2;
            m_Info.FrameHeight = m_Texture.Height;

            m_Button = SpriteFactory.GetInstance().GetObject(SpriteID.TOGGLE_SPRITE, m_Info, m_TextureName) as ISprite;
            RenderDrawer.GetInstance().AddSprite(m_Button);
        }

//         public override void Draw(SpriteBatch spriteBatch, GameTime gametime)
//         {
//             spriteBatch.Begin();
//             spriteBatch.Draw(
//                 ResourceManager.GetInstance().GetContent(m_TextureName),
//                 m_Info.WorldLocation,
//                 new Rectangle(0, 0, m_Info.FrameWidth, m_Info.FrameHeight),
//                 Color.White);
//             spriteBatch.End();
//             m_Button.Draw(spriteBatch, gametime);
//         }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            IMouseInputHandler input = MouseInputHandler.GetInputHandler();
            MouseState mouse = input.MouseState;

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                if (SourceCollision.Contains(mouse.X, mouse.Y))
                {
                    (m_Button as IButton).SetButtonState(Button_State.PRESSED);
                    if (gameObject.IsAnimationPlay())
                        gameObject.AnimationChange(CharacterState.Sliding);
                }
            }
            else if ((mouse.LeftButton == ButtonState.Released) && (input.PreviousMouseState.LeftButton == ButtonState.Pressed))
            {
                if (SourceCollision.Contains(mouse.X, mouse.Y))
                {
                    (m_Button as IButton).SetButtonState(Button_State.NONE);

                    gameObject.AnimationChange(CharacterState.Running);
                }
            }
        }

    }
}
